package com.dashanchessgame.gameui;

import com.dashanchessgame.chess.Chess;
import com.dashanchessgame.chess.RangeChecker;
import com.dashanchessgame.tools.ImageTool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.concurrent.TimeUnit;


/**
 * 棋盘画布布局
 *
 * @author 37460
 */
public class CanvasChess extends JPanel implements MouseListener {
    public static final int CHESS_ROW = 10;//棋盘行数
    public static final int CHESS_LINE = 9;//棋盘列数
    int[][] map = new int[CHESS_ROW][CHESS_LINE];


    public boolean isBlack = false; //判断当前谁下子

    public int selectR = -1;//初始化
    public int selectC = -1;//初始化坐标


    public CanvasChess() {
        initMap();
        this.addMouseListener(this);
    }

    //  鼠标鼠标点击监听

    private Image mainGif;
    ChessManager manager = new ChessManager();

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //System.out.println("paint called ----------------");
        //System.out.println("宽"+w+"  高"+h);
        g.setFont(new Font("宋体", Font.BOLD, 30));
        g.drawImage(mainGif, 0, 0, null);

        for (int r = 0; r < map.length; r++) {
            for (int c = 0; c < map[r].length; c++) {
                safelyDraw(g, manager.getImage(map[r][c]), 25 + 58 * c, r * 58 + 25);
            }
        }
        g.drawRect(25 + 58 * selectC, 25 + 58 * selectR, 58, 58);
        g.drawString(isBlack ? "黑方下棋" : "红方下棋", 220, 650);
    }

    protected void showFail(String msg) {
        System.out.println(msg);
    }


    //初始化界面棋盘每个点对应的值
    //1000+gype属性值
    public void initMap() {
        map = new int[][]{
                {1000 + 'C', 1000 + 'M', 1000 + 'X', 1000 + 'S', 1000 + 'J', 1000 + 'S', 1000 + 'X', 1000 + 'M', 1000 + 'C'},
                {0, 0, 0, 0, 0, 0, 0, 0, 0,},
                {0, 1000 + 'P', 0, 0, 0, 0, 0, 1000 + 'P', 0},
                {1000 + 'B', 0, 1000 + 'B', 0, 1000 + 'B', 0, 1000 + 'B', 0, 1000 + 'B'},
                {0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0},
                {'B', 0, 'B', 0, 'B', 0, 'B', 0, 'B'},
                {0, 'P', 0, 0, 0, 0, 0, 'P', 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0},
                {'C', 'M', 'X', 'S', 'J', 'S', 'X', 'M', 'C'}};
        //System.out.println(map[9][4]);
        mainGif = ImageTool.loadImage("main.gif");
        manager.loadAllImage();  //加载所有图片
    }

    public void safelyDraw(Graphics g, Image img, int x, int y) {
        try {
            if (img == null) {
                //System.err.println("img = null ,skip drawing "+x+" , "+y);
            }
            g.drawImage(img, x, y, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            int x = e.getX();
            int y = e.getY();
            //System.out.println(e.getX()+","+e.getY()+"["+x+","+y+"]");
            int r = (y - 25) / 58; //移动位置y左边
            int c = (x - 25) / 58;//移动位置的x坐标
            //System.out.println("r = "+r+", c = "+c);
            //判断攻守防
            if (selectC == -1 && map[r][c] != 0) {
                if (RangeChecker.selectInRange(map, r, c, isBlack)) {//isBlack 是 红方
                    selectR = r;
                    selectC = c;
                    repaint();
                } else {
                    showFail("请选择己方棋子");
                }
                return;// 选子阶段结束
            }

            boolean valid = RangeChecker.inRange(map, selectR, selectC, r, c, isBlack);
            if (!valid) {
                showFail("下子失败");
            }

            Chess chess = manager.getTheChess(map[selectR][selectC]);
            valid = chess.chessCheck(map, selectR, selectC, r, c, isBlack);
            //校验棋子是敌是友 如果去的位置是友方贼判断下子失败
            if (!valid || (map[selectR][selectC] < 1000 && map[r][c] < 1000 && map[r][c] != 0) || (map[selectR][selectC] > 1000 && map[r][c] > 1000 && map[r][c] != 0)) {
                showFail("下子失败,规则不允许");
                selectR = -1;
                selectC = -1;
                repaint();
                return ;
            }

            map[r][c] = map[selectR][selectC];
            map[selectR][selectC] = 0;
            selectR = -1;
            selectC = -1;
//
//            try {
//                TimeUnit.HOURS.sleep(1);
//            } catch (InterruptedException ex) {
//                throw new RuntimeException(ex);
//            }

            //对方来下
            isBlack = !isBlack;
            repaint();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
